import java.util.*;

public class Solution {

    //
    public int purchasePlans(int[] nums, int target) {
        Arrays.sort(nums);

        int mod = 1_000_000_007;
        int left = 0, right = nums.length - 1, ans = 0;
        while(left < right){
            if(nums[left] + nums[right] <= target){
                ans += right - left;
                left++;
            }else{
                right--;
            }
            ans %= mod;
        }
        return ans % mod;
    }

    //
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);

        List<List<Integer>> ans = new ArrayList<>();

        for (int i = 0; i < nums.length; ) {
            int x = -nums[i];
            if (x < 0) { break;}

            int left = i + 1, right = nums.length - 1;
            while (left < right) {
                if (nums[left] + nums[right] < x) {
                    left++;
                    while (left < right && nums[left] == nums[left - 1]) { left++;}
                } else if (nums[left] + nums[right] > x) {
                    right--;
                    while (left < right && nums[right] == nums[right + 1]) { right--;}
                } else {
                    ans.add(new ArrayList<Integer>(Arrays.asList(nums[i], nums[left], nums[right])));
                    left++;
                    right--;
                    while (left < right && nums[left] == nums[left - 1]) { left++;}
                    while (left < right && nums[right] == nums[right + 1]) { right--;}
                }
            }
            i++;
            while (i < nums.length && nums[i] == nums[i - 1]) { i++;}
        }
        return ans;
    }

    //
    public int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);

        int ans = Integer.MAX_VALUE;
        for(int i = 0; i < nums.length; i++){
            int left = i + 1, right = nums.length - 1;
            while(left < right){
                int x = nums[i] + nums[left] + nums[right];
                if(x > target){
                    right--;
                    ans = Math.abs(ans - target) > Math.abs(x - target) ? x : ans;
                }else if(x < target){
                    left++;
                    ans = Math.abs(ans - target) > Math.abs(x - target) ? x : ans;
                }else{
                    ans = target;
                    break;
                }
            }
        }
        return ans;
    }

    //
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);

        List<List<Integer>> ans = new ArrayList<>();

        for(int i = 0; i < nums.length; ){
            for(int j = i + 1; j < nums.length; ){
                int left = j + 1, right = nums.length - 1;
                while(left < right){
                    long x = (long) target - nums[i] - nums[j];
                    if(nums[left] + nums[right] > x){
                        right--;
                    }else if(nums[left] + nums[right] < x){
                        left++;
                    }else{
                        ans.add(new ArrayList<>(Arrays.asList(nums[i], nums[j], nums[left], nums[right])));
                        left++;
                        right--;
                        while(left < right && nums[left] == nums[left - 1]) { left++;}
                        while(left < right && nums[right] == nums[right + 1]) { right--;}
                    }
                }
                j++;
                while(j < nums.length && nums[j] == nums[j - 1]) { j++;}
            }
            i++;
            while(i < nums.length && nums[i] ==nums[i - 1]) { i++;}
        }
        return ans;
    }
}
